package com.bootdo.system.controller;

import com.bootdo.common.controller.BaseController;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Date;

/**
 * 文件管理
 *
 * @auther: Ace Lee
 * @date: 2019/10/26 10:43
 */
@RestController
@RequestMapping("/files")
public class PDFFileController extends BaseController {

    @Value("${bootdo.uploadPath}")
    private String filePath;

    @GetMapping("pdf")
    public void showPDF(@RequestParam("filename") String filename,
                        HttpServletResponse response) throws IOException {
        if (StringUtils.isEmpty(filename)) {
            return;
        }

        String path = filePath + filename;
        File file = new File(path);
        if (!file.exists()) {
            return;
        }

        //【分片】https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Accept-Ranges
        response.setHeader("Accept-Ranges", "bytes");
        //【跨域】
        response.setHeader("Access-Control-Allow-Origin", "*");
        //【分片】Content-Length 表示资源内容长度，即：文件大小
        response.setHeader("Content-Length", String.valueOf(file.length()));
        Date date = new Date();

        //【浏览器缓存】设置缓存周期为24个小时
        Long cacheCycle = (long) (24 * 60 * 60 * 1000);

        Long time = date.getTime() + cacheCycle;

        //【浏览器缓存】Last-Modified:页面的最后生成时间
        response.setDateHeader("Last-Modified", date.getTime());

        //【浏览器缓存】Expires:过时期限值
        response.setDateHeader("Expires", time);

        //【浏览器缓存】Cache-Control来控制页面的缓存与否,public:浏览器和缓存server都能够缓存页面信息
        response.setHeader("Cache-Control", "public");

        //【浏览器缓存】Pragma:设置页面是否缓存，为Pragma则缓存，no-cache则不缓存
        response.setHeader("Pragma", "Pragma");
        //【分片】
        response.setHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));// 设置在下载框默认显示的文件名
        //【分片】Content-Range 表示响应了多少数据，格式为：[要下载的开始位置]-[结束位置]/[文件总大小]
//        response.setHeader("Content-Range", "bytes " + startByte + "-" + endByte + "/" + file.length());
        //【浏览器缓存】
        response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
        // 读出文件到response
        // 这里是先需要把要把文件内容先读到缓冲区
        // 再把缓冲区的内容写到response的输出流供用户下载
        FileInputStream fileInputStream = new FileInputStream(file);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        OutputStream outputStream = response.getOutputStream();
        byte buffer[] = new byte[1024];
        int len = 0;
        while ((len = bufferedInputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, len);
        }
        // 人走带门
        bufferedInputStream.close();
        outputStream.flush();
        outputStream.close();

    }

}
